import { defineStore } from 'pinia'
import axios from 'axios'

export const useAnalyticsStore = defineStore('analytics', {
  state: () => ({
    ratingDistribution: [],
    genreDistribution: [],
    loading: false,
    error: null
  }),

  actions: {
    async fetchAnalytics() {
      this.loading = true
      this.error = null
      try {
        const [ratingResponse, genreResponse] = await Promise.all([
          axios.get('http://localhost:8000/api/analytics/ratings'),
          axios.get('http://localhost:8000/api/analytics/genres')
        ])
        this.ratingDistribution = ratingResponse.data
        this.genreDistribution = genreResponse.data
      } catch (error) {
        this.error = '获取数据分析失败'
        console.error('Failed to fetch analytics:', error)
      } finally {
        this.loading = false
      }
    }
  }
}) 